#include "IFUException.h"
#include "Image.h"
#include "ImageSlice.h"

#include <string>
#include <iostream>

using namespace std;

Image::Image() {
  this->pixels = 0;
}

Image::Image(const string &filename) {
  this->pixels = 0;
  this->filename = filename;
}

Image::~Image() {
}

const string &Image::getFilename() {
  return this->filename;
}

int Image::getNrows() {
  return this->nRows;
}

int Image::getNcols() {
  return this->nCols;
}

float **Image::getPixels() {
  return this->pixels;
}

float Image::getPixel(int row, int col) {
  return this->pixels[row][col];
}

ImageSlice *Image::getColumn(int col) {

  ImageSlice *slice = new ImageSlice(this->nRows, col);

  for (int i = 0; i < this->nRows; i++ )
    slice->set(i, (this->pixels[i][col]));

  return slice;

}

ImageSlice *Image::getColumn(int col, int avgCols) {

  if ( (avgCols%2) != 1 )
    throw IFUException("Can't use even number of columns", "Image");

  ImageSlice *slice = new ImageSlice(this->nRows, col);

  int i, start, end;
  start = col - (avgCols-1) / 2 ;
  
  if ( start < 0 )
    start = 0 ;
  
  end = col + (avgCols-1) / 2 ;
  
  if ( end > this->nCols )
    end = col ;
  
  for (int x = 0; x < nRows; x++) {

    float avg;
    avg = 0.0 ;

    for (i = start; i <= end; i++)
      avg += pixels[x][i] ;

    slice->set(x, (avg / avgCols));

  }

  return slice;

}

ImageSlice *Image::getRow(int row) {
  return (new ImageSlice(row));
}

ImageSlice *Image::getRow(int row, int avgRows) {
  cout << avgRows << endl;
  return (new ImageSlice(row));
}

void Image::setFilename(const string &filename) {
  this->filename = filename;
}

void Image::setPixel(int row, int col, float value) {
  this->pixels[row][col] = value;
}

void Image::setNrows(int nRows) {
  this->nRows = nRows;
}

void Image::setNcols(int nCols) {
  this->nCols = nCols;
}
